﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevComponents.DotNetBar;
using DevComponents.AdvTree;
using System.Collections;

namespace NomCafe
{
    public partial class frmChangeTable : DevComponents.DotNetBar.Office2007RibbonForm
    {
        private Main_Cashier mainCashier;

        public frmChangeTable(Main_Cashier mCashier)
        {
            InitializeComponent();
            mainCashier = mCashier;
        }

        private void frmChangeTable_Load(object sender, EventArgs e)
        {
            panelEx1.Text = "Bàn đang chọn : " + Lib_Cashier.currentNodeName + "(" + Lib_Cashier.currentRootName + ")";
            Load_InformationTable();
        }

        private void Load_InformationTable()
        {
            // Define group node style
            ElementStyle groupStyle = new ElementStyle();

            // Define sub-item style, simply to make text gray
            ElementStyle subItemStyle = new ElementStyle();
            subItemStyle.TextColor = Color.Gray;
            subItemStyle.Name = "subitemstyle";

            //************************************************
            // Setup data-bound tree with auto-grouping
            //************************************************

            // Area group style
            groupStyle = new ElementStyle();
            groupStyle.TextColor = Color.SlateGray;
            groupStyle.Font = new Font(advTreeChangeTable.Font.FontFamily, 9f, FontStyle.Bold);
            groupStyle.Name = "groupstyle";
            advTreeChangeTable.Styles.Add(groupStyle);

            // Style for contact name
            subItemStyle = new ElementStyle();
            subItemStyle.TextColor = Color.SlateGray;
            subItemStyle.Name = "subitemstyle";
            advTreeChangeTable.Styles.Add(subItemStyle);

            // Style for phone cell
            subItemStyle = new ElementStyle();
            subItemStyle.TextColor = Color.Maroon;
            subItemStyle.Font = new Font(advTreeChangeTable.Font.FontFamily, 10f, FontStyle.Bold);
            subItemStyle.TextAlignment = eStyleTextAlignment.Center;
            subItemStyle.Name = "subitemphone";
            advTreeChangeTable.Styles.Add(subItemStyle);
            #region List customer
            ArrayList customers = new ArrayList();
            NomClasses.NomDataContext nc = new NomClasses.NomDataContext();
            System.Data.Linq.Table<NomClasses.nom_area> areas = nc.nom_areas;
            foreach (NomClasses.nom_area arr in areas)
            {
                foreach (NomClasses.nom_table tb in arr.nom_tables)
                {
                    customers.Add(new Customer("", "", arr.name, tb.code));
                }
            }
            #endregion
            advTreeChangeTable.GroupingMembers = "Country";
            advTreeChangeTable.DisplayMembers = "Contact,Phone";
            advTreeChangeTable.GroupNodeCreated += new DevComponents.DotNetBar.Controls.DataNodeEventHandler(AdvTreeTableCoffeeGroupNodeCreated);
            advTreeChangeTable.DataNodeCreated += new DevComponents.DotNetBar.Controls.DataNodeEventHandler(AdvTreeTableCoffeeDataNodeCreated);
            advTreeChangeTable.TileSize = new Size(64, 50);
            advTreeChangeTable.DataSource = customers;
        }

        void AdvTreeTableCoffeeDataNodeCreated(object sender, DevComponents.DotNetBar.Controls.DataNodeEventArgs e)
        {
            string currentTable = e.Node.Parent.Text + "," + e.Node.Cells[1].Text;
            string[] selectTable = Lib_Cashier.listSelectTable.Split(';');
            foreach (string tables in selectTable)
            {
                if (tables == currentTable)
                {
                    e.Node.Visible = false;
                    e.Node.ImageAlignment = eCellPartAlignment.CenterBottom;
                    e.Node.Cells[1].StyleNormal = advTreeChangeTable.Styles["subitemstyle"];
                    e.Node.Cells[1].StyleNormal = advTreeChangeTable.Styles["subitemphone"];
                    break;
                }
                else
                {
                    e.Node.ImageIndex = 0;
                    e.Node.ImageAlignment = eCellPartAlignment.CenterBottom;
                    e.Node.Cells[1].StyleNormal = advTreeChangeTable.Styles["subitemstyle"];
                    e.Node.Cells[1].StyleNormal = advTreeChangeTable.Styles["subitemphone"];
                }                
            }            
        }

        void AdvTreeTableCoffeeGroupNodeCreated(object sender, DevComponents.DotNetBar.Controls.DataNodeEventArgs e)
        {
            e.Node.Style = advTreeChangeTable.Styles["groupstyle"];
        }

        private void btnCloseWindows_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void advTreeTableCoffee_NodeMouseMove(object sender, TreeNodeMouseEventArgs e)
        {
            if (e.Node.Style != advTreeChangeTable.Styles["groupstyle"] && (e.Node.ImageIndex != 3))
            {
                e.Node.ImageIndex = 1;
            }
        }

        private void advTreeTableCoffee_NodeMouseLeave(object sender, TreeNodeMouseEventArgs e)
        {
            if ((e.Node.Style != advTreeChangeTable.Styles["groupstyle"]) && (e.Node.ImageIndex != 3))
            {
                e.Node.ImageIndex = 0;
            }
        }

        private void advTreeTableCoffee_NodeClick(object sender, TreeNodeMouseEventArgs e)
        {
            Lib_Cashier.nodeNameChangeTable = "";
            // Lấy thông tin node table đã chọn
            Lib_Cashier.nodeNameChangeTable = e.Node.Parent.Text + "," + e.Node.Cells[1].Text;
                        
            e.Node.ImageIndex = 3;
            advTreeChangeTable.Enabled = false;
            btnChangeTable.Enabled = true;
            btnSelectAnotherTable.Enabled = true;
        }

        #region Customer Class
        internal class Customer
        {
            /// <summary>
            /// Initializes a new instance of the Customer class.
            /// </summary>
            /// <param name="company"></param>
            /// <param name="contact"></param>
            /// <param name="country"></param>
            /// <param name="phone"></param>
            public Customer(string company, string contact, string country, string phone)
            {
                Company = company;
                Contact = contact;
                Country = country;
                Phone = phone;
            }
            private string _Company;
            public string Company
            {
                get { return _Company; }
                set
                {
                    _Company = value;
                }
            }
            private string _Contact;
            public string Contact
            {
                get { return _Contact; }
                set
                {
                    _Contact = value;
                }
            }
            private string _Country;
            public string Country
            {
                get { return _Country; }
                set
                {
                    _Country = value;
                }
            }
            private string _Phone;
            public string Phone
            {
                get { return _Phone; }
                set
                {
                    _Phone = value;
                }
            }
        }
        #endregion

        private void btnSelectAnotherTable_Click(object sender, EventArgs e)
        {
            advTreeChangeTable.Enabled = true;
            Load_InformationTable();
        }

        private void btnChangeTable_Click(object sender, EventArgs e)
        {
            // Gọi method changTable ở form Main_Cashier
            mainCashier.ChangeTable();
            this.Close();
        }
    }
}
